import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_chat_detail/lf_chat_detail_logic.dart';
import 'package:largefriends/pages/lf_profile/lf_profile_hidden.dart';
import 'package:largefriends/pages/lf_profile/lf_profile_loading.dart';
import 'package:largefriends/res.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/widgets/dialog_action_sheet.dart';
import 'package:lottie/lottie.dart';

import '../../net/lf_api_util.dart';
import '../../res/lf_widget_ext.dart';
import '../../res/strings/lf_strings.dart';
import '../../widgets/lf_app_bar.dart';
import '../lf_base_controller.dart';
import 'lf_profile_logic.dart';
import 'lf_profile_widget.dart';

class LfProfilePage extends StatefulWidget {
  const LfProfilePage({super.key});

  @override
  State<LfProfilePage> createState() => _LfProfilePageState();
}

class _LfProfilePageState extends State<LfProfilePage> {
  late String uid;
  @override
  void initState() {
    uid = Get.arguments['uid'];
    Get.create<LfProfileLogic>(() {
      return LfProfileLogic();
    }, tag: uid, permanent: false);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LfProfileLogic>(
        builder: (logic) {
          return WillPopScope(
              child: Scaffold(
                appBar: null,
                body: _getBody(logic),
                extendBodyBehindAppBar: true,
              ),
              onWillPop: () async {
                Get.back();
                return false;
              });
        },
        tag: uid,
        global: false,
        init: Get.find<LfProfileLogic>(tag: uid));
  }

  Widget _getBody(LfProfileLogic logic) {
    if (logic.hiddenCode > 0) {
      return const LfProfileHidden();
    } else if (logic.state == LoadingState.loading) {
      return const LfProfileLoading();
    } else if (logic.userEntity != null &&
        logic.state == LoadingState.success) {
      return Stack(
        children: [
          Positioned.fill(
              child: LfProfileWidget(
            userEntity: logic.userEntity!, moreCall: (){
              _moreAction(logic);
              },
          )),
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              height: 120.h,
              width: ScreenUtil().screenWidth,
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [color000000.withAlpha(0), color000000])),
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Padding(
              padding: EdgeInsets.only(
                bottom: 34.h,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  InkWell(
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.blueAccent,
                          shape: BoxShape.circle,
                          boxShadow: [
                            BoxShadow(
                                color: Colors.blueAccent,
                                spreadRadius: 1.r,
                                blurRadius: 2.r)
                          ]),
                      width: 56.r,
                      height: 56.r,
                      padding: EdgeInsets.all(6.r),
                      child: Lottie.asset(Res.message_icon_anim, repeat: false),
                    ),
                    onTap: () {
                      toChatPage(uid: logic.uid, userEntity: logic.userEntity);
                    },
                  ),
                  logic.userEntity!.liked == 1 ? empty : 60.w.sbw,
                  logic.userEntity!.liked == 1
                      ? empty
                      : InkWell(
                          child: Container(
                            decoration: BoxDecoration(
                                color: colorFF007F,
                                shape: BoxShape.circle,
                                boxShadow: [
                                  BoxShadow(
                                      color: colorFF007F,
                                      spreadRadius: 1.r,
                                      blurRadius: 2.r)
                                ]),
                            width: 56.r,
                            height: 56.r,
                            child: Image.asset(
                              logic.userEntity!.liked == 1
                                  ? Res.icon_like
                                  : Res.icon_like,
                              width: 54.r,
                              height: 54.r,
                            ),
                          ),
                          onTap: () {
                            if (logic.userEntity!.liked == 1) {
                              unlikeUser(user: logic.userEntity!);
                            } else {
                              likeUser(user: logic.userEntity!);
                            }
                          },
                        ),
                ],
              ),
            ),
          ),
        ],
      );
    } else {
      return Container();
    }
  }

  _moreAction(LfProfileLogic logic) {
    showActionSheet(actions: [
      ActionSheetItem(
          action: () {
            logic.report();
          },
          title: strReport.tr.toUpperCase()),
      ActionSheetItem(
          action: () {
            logic.block();
          },
          title: strBlock.tr),
    ]);
  }
}
